#
#  Copyright 2011-2012 Gregory Banks
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
PACKAGE_NAME=	@PACKAGE_NAME@
PACKAGE_TARNAME=@PACKAGE_TARNAME@
PACKAGE_VERSION=@PACKAGE_VERSION@

prefix=		@prefix@
exec_prefix=	@exec_prefix@
includedir=	@includedir@
libdir=		@libdir@
datarootdir=	@datarootdir@
mandir=		@mandir@
pkgconfigdir=	$(libdir)/pkgconfig

libxml_CFLAGS=	@libxml_CFLAGS@
libbfd_CFLAGS=	@libbfd_CFLAGS@
valgrind_CFLAGS=@valgrind_CFLAGS@
platform_CFLAGS=@platform_CFLAGS@
platform_SOURCE=@platform_SOURCE@
BUILD_DOCS=@BUILD_DOCS@

CC=		g++
CXX=		g++
CDEBUGFLAGS=	-g
COPTFLAGS=	-O0
CDEFINES=	-I. $(platform_CFLAGS) $(libxml_CFLAGS) $(libbfd_CFLAGS) $(valgrind_CFLAGS)
CWARNFLAGS=	-Wall -Wextra -Werror
CFLAGS=		$(CDEBUGFLAGS) $(COPTFLAGS) $(CWARNFLAGS) $(CDEFINES)
CXXFLAGS=	$(CFLAGS)
INSTALL=	@INSTALL@
INSTALL_DATA=	@INSTALL_DATA@
MKDIRP=		$(INSTALL) -m 0755 -d
RANLIB=		@RANLIB@
depdir=		.deps

SUBDIRS_POST=	tests

all clean distclean check install docs:
	@$(MAKE) $@-local
	@for dir in $(SUBDIRS_POST) ; do $(MAKE) -C $$dir $@ ; done

install check: all

ifeq ($(BUILD_DOCS), yes)
all-local: libnovaprova.a obs-metadata doc/.doxy-stamp
else
all-local: libnovaprova.a obs-metadata
endif

libnovaprova_SOURCE= \
		np.c \
		isyslog.c iassert.c icunit.c iexit.c uasserts.c iexcept.c \
		main.c \
		np/child.cxx \
		np/classifier.cxx \
		np/event.cxx \
		np/job.cxx \
		np/junit_listener.cxx \
		np/plan.cxx \
		np/proxy_listener.cxx \
		np/runner.cxx \
		np/spiegel/dwarf/abbrev.cxx \
		np/spiegel/dwarf/compile_unit.cxx \
		np/spiegel/dwarf/entry.cxx \
		np/spiegel/dwarf/enumerations.cxx \
		np/spiegel/dwarf/reference.cxx \
		np/spiegel/dwarf/state.cxx \
		np/spiegel/dwarf/string_table.cxx \
		np/spiegel/dwarf/value.cxx \
		np/spiegel/dwarf/walker.cxx \
		np/spiegel/intercept.cxx \
		np/spiegel/mapping.cxx \
		$(addprefix np/spiegel/platform/,$(platform_SOURCE)) \
		np/spiegel/spiegel.cxx \
		np/testmanager.cxx \
		np/testnode.cxx \
		np/text_listener.cxx \
		np/types.cxx \
		np/util/common.cxx \
		np/util/filename.cxx \
		np/util/profile.cxx \
		np/util/tok.cxx \

libnovaprova_PRIVHEADERS= \
		np/spiegel/common.hxx \
		np/spiegel/dwarf/abbrev.hxx \
		np/spiegel/dwarf/compile_unit.hxx \
		np/spiegel/dwarf/entry.hxx \
		np/spiegel/dwarf/enumerations.hxx \
		np/spiegel/dwarf/reader.hxx \
		np/spiegel/dwarf/reference.hxx \
		np/spiegel/dwarf/section.hxx \
		np/spiegel/dwarf/state.hxx \
		np/spiegel/dwarf/string_table.hxx \
		np/spiegel/dwarf/value.hxx \
		np/spiegel/dwarf/walker.hxx \
		np/spiegel/intercept.hxx \
		np/spiegel/mapping.hxx \
		np/spiegel/platform/common.hxx \
		np/spiegel/spiegel.hxx \
		np/util/common.hxx \
		np/util/filename.hxx \
		np/util/profile.hxx \
		np/util/tok.hxx \
		np_priv.h \

libnovaprova_HEADERS= \
		np.h \
		np/child.hxx \
		np/classifier.hxx \
		np/event.hxx \
		np/job.hxx \
		np/junit_listener.hxx \
		np/listener.hxx \
		np/plan.hxx \
		np/proxy_listener.hxx \
		np/runner.hxx \
		np/testmanager.hxx \
		np/testnode.hxx \
		np/text_listener.hxx \
		np/types.hxx \

libnovaprova_OBJS= \
	$(patsubst %.c,%.o,$(filter %.c,$(libnovaprova_SOURCE))) \
	$(patsubst %.cxx,%.o,$(filter %.cxx,$(libnovaprova_SOURCE)))
#
# Automatic dependency tracking
libnovaprova_DFILES= \
	$(patsubst %.c,$(depdir)/%.d,$(filter %.c,$(libnovaprova_SOURCE))) \
	$(patsubst %.cxx,$(depdir)/%.d,$(filter %.cxx,$(libnovaprova_SOURCE))) \

-include $(libnovaprova_DFILES)

%.o: %.cxx
	@mkdir -p $(dir $(depdir)/$(patsubst %.cxx,%.d,$<))
	$(COMPILE.C) -MMD -MF $(depdir)/$(patsubst %.cxx,%.d,$<) -MT $@ -o $@ $<

%.o: %.c
	@mkdir -p $(dir $(depdir)/$(patsubst %.c,%.d,$<))
	$(COMPILE.c) -MMD -MF $(depdir)/$(patsubst %.c,%.d,$<) -MT $@ -o $@ $<

libnovaprova.a: $(libnovaprova_OBJS)
	$(AR) $(ARFLAGS) libnovaprova.a $(libnovaprova_OBJS)

# Do the part of the docs build that just runs Doxygen.
# We can rely on this being present even for TOT builds.
# The remainder of the docs builds rely on more advanced
# tools like Sphinx and Breathe that are harder to get.
doc/.doxy-stamp: Doxyfile
	$(RM) -r doc/man doc/xml
	doxygen
	$(RM) doc/man/man*/_*
	touch doc/.doxy-stamp

# Builds manual tarball at release time.  This tarball will be checked
# into OBS, then built into binary release packages by the REL version
# of novaprova.spec.  This convoluted pipeline is intended to avoid the
# requirement to have a working version of Sphinx and Breathe on all
# the release platforms in OBS.
MANUAL_TARBALL=	$(PACKAGE_TARNAME)-manual-$(PACKAGE_VERSION).tar.bz2
MANUAL_DIR=     $(PACKAGE_TARNAME)-manual-$(PACKAGE_VERSION)
docs-local: doc/.doxy-stamp
	./prune-doxygen-xml.pl doc/xml
	make -C doc/manual clean html
	$(RM) -r doc/manual/_build/html/_sources
	$(RM) -r $(MANUAL_DIR)
	mkdir -p $(MANUAL_DIR)/manual
	cp -r doc/manual/_build/html $(MANUAL_DIR)/manual/html
	tar -cjvf $(MANUAL_TARBALL) $(MANUAL_DIR)
	$(RM) -r $(MANUAL_DIR)
	@echo "Manual in $(MANUAL_TARBALL)"

install:

install-local:
	$(MKDIRP) $(DESTDIR)$(includedir)/novaprova/np
	for hdr in $(libnovaprova_HEADERS) ; do \
	    $(INSTALL_DATA) $$hdr $(DESTDIR)$(includedir)/novaprova/$$hdr ;\
	done
	$(MKDIRP) $(DESTDIR)$(libdir)
	$(INSTALL_DATA) libnovaprova.a $(DESTDIR)$(libdir)/libnovaprova.a
	$(RANLIB) $(DESTDIR)$(libdir)/libnovaprova.a
	$(MKDIRP) $(DESTDIR)$(pkgconfigdir)
	$(INSTALL_DATA) novaprova.pc $(DESTDIR)$(pkgconfigdir)
ifeq ($(BUILD_DOCS), yes)
	$(MKDIRP) $(DESTDIR)$(mandir)/man3
	$(INSTALL_DATA) doc/man/man3/np*.3 doc/man/man3/NP*.3 $(DESTDIR)$(mandir)/man3
endif

clean-local:
	$(RM) libnovaprova.a $(libnovaprova_OBJS)

distclean-local: clean-local
	$(RM) -r doc/man
	$(RM) -r $(depdir)
	$(RM) novaprova.pc
	$(RM) config.status configure Makefile Doxyfile

check-local:

DIST_TARBALL=	$(PACKAGE_TARNAME)-$(PACKAGE_VERSION).tar.bz2
DISTDIR=	$(PACKAGE_NAME)-$(PACKAGE_VERSION)

DIST_FILES= $(shell git ls-files) \

DIST_DIRS= \
	doc/man \
	doc/manual \

dist: docs
	$(RM) -r $(DISTDIR)
	mkdir $(DISTDIR)
	for file in $(DIST_FILES) `find $(DIST_DIRS) -type f` ; do \
	    mkdir -p `dirname $(DISTDIR)/$$file` ;\
	    ln $$file $(DISTDIR)/$$file ;\
	done
	tar -chjvf $(DIST_TARBALL) $(DISTDIR)
	$(RM) -r $(DISTDIR)

#
# The files in build/obs/{tot,rel}/ are unique in that they are *both*
# generated *and* committed in git.  They are generated because we need
# two sets of nearly identical files, and they are in git because
# OpenBuildService needs them there.
#
OBS_SOURCE=	debian.changelog debian.control \
		debian.rules novaprova.changes \
		novaprova.dsc novaprova.spec
obs-metadata:	$(addprefix build/obs/rel/,$(OBS_SOURCE)) $(addprefix build/obs/tot/,$(OBS_SOURCE))

build/obs/tot/%: build/obs/% build/obs/ifdef.py
	@mkdir -p $(@D)
	build/obs/ifdef.py -Dtot -Dtop_of_tree < $< > $@

build/obs/rel/%: build/obs/% build/obs/ifdef.py
	@mkdir -p $(@D)
	build/obs/ifdef.py -Drel -Drelease < $< > $@

